import React from "react";
import {Form, Input} from "antd";
import BaseComponent from "../../../redare/components/BaseComponent";
import DictionaryPresenter from "../../../.gen/presenter/DictionaryPresenter";

export default class DictionaryAddForm extends BaseComponent {

    formRef = React.createRef();

    constructor(props) {
        super(props);
        this.dicPresenter = new DictionaryPresenter(this)
    }

    componentDidMount() {
        this._loadData();
    }

    _loadData() {
        let {editObj} = this.props;
        if (editObj && editObj.isEdit) {
            this.dicPresenter.getDictionary({id: editObj.editId}, {},(result) => {
                let {data} = result;
                this._setData(data)
            })
        }
    }

    _setData(data) {
        this.formRef.current.setFieldsValue({
            dicName: data.dicName,
            dicCode: data.dicCode,
            dicValue: data.dicValue,
            des: data.des
        })
    }

    render() {
        let rules = function (title, type) {
            type = type ? type : "输入";
            return [{required: true, message: '请' + type + title}];
        };
        let {editObj} = this.props;
        let disable = editObj && editObj.isEdit;
        return (
            <Form ref={this.formRef} className="add-form margin-bottom"
                  labelAlign='center' labelCol={{span: 6}} wrapperCol={{span: 16}}>

                <Form.Item name="dicCode" label="字典编码" rules={rules("字典编码")}>
                    <Input autoComplete={"off"} disabled={disable} placeholder="请输入字典编码"/>
                </Form.Item>

                <Form.Item name="dicName" label="字典名称" rules={rules("字典名称")}>
                    <Input autoComplete={"off"} placeholder="请输入字典名称"/>
                </Form.Item>

                <Form.Item name="dicValue" label="字典取值">
                    <Input autoComplete={"off"} placeholder="请填写字典取值"/>
                </Form.Item>

                <Form.Item name="des" label="描述">
                    <Input autoComplete={"off"} placeholder="请填写描述内容"/>
                </Form.Item>
            </Form>
        )
    }


}